from ast import main
import requests
from csv_utils import get_ips


def get_auth(zabbix_url, headers, username, password):
    login_payload = {
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            "username": username,
            "password": password
        },
        "id": 1
    }
    response = requests.post(zabbix_url, headers=headers, json=login_payload)
    auth_token = response.json().get('result')
    print("Auth token:", auth_token)

    return auth_token


def get_hosts(zabbix_url, headers, auth_token, host):
    get_hosts_payload = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "output": ["hostid", "host", "name"],
            "filter": {
                "host": [
                    host
                ]
            }
        },
        "auth": auth_token,
        "id": 2
    }

    response = requests.post(zabbix_url, headers=headers, json=get_hosts_payload)
    hosts = response.json().get('result')
    return hosts

def get_hosts_from_group(zabbix_url, headers, auth_token, group):
    get_hosts_payload = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "output": ["hostid", "host", "name"],
            "filter": {
                "group": [
                    group
                ]
            }
        },
        "auth": auth_token,
        "id": 2
    }

    response = requests.post(zabbix_url, headers=headers, json=get_hosts_payload)
    hosts = response.json().get('result')
    return hosts

def update_hosts(zabbix_url, headers, auth_token, hostid, tags: list):
    update_hosts_payload = {
        "jsonrpc": "2.0",
        "method": "host.update",
        "params": {
            "hostid": int(hostid),
            "tags": [
                {
                "tag": tags[0],
                "value": tags[1]
                },
                # 这里可以添加多个标签，但之前的标签会被覆盖。
                # {
                #     "tag": 'aaa',
                #     "value": tags[3]
                # }
            ]
        },
        "auth": auth_token,
        "id": 1
    }
    r = requests.post(zabbix_url, headers=headers, json=update_hosts_payload)
    print(r.json())

# update_hosts(hostid)

def logout(zabbix_url, headers, auth_token):
    logout_payload = {
        "jsonrpc": "2.0",
        "method": "user.logout",
        "params": [],
        "auth": auth_token,
        "id": 3
    }

    requests.post(zabbix_url, headers=headers, json=logout_payload)
    print("Logged out successfully.")


def main():
    zabbix_url = "http://10.104.14.27/api_jsonrpc.php"
    username = "Admin"
    password = "zabbix"
    headers = {"Content-Type": "application/json-rpc"}

    auth_token = get_auth(zabbix_url, headers, username, password)

    # hosts = get_hosts_from_group(zabbix_url, headers, auth_token, "有线网")
    # ips = get_ips('e:\\ips.csv')
    # for host in hosts:
    #     ip = '.'.join(host['host'].split('.')[2:])
    #     switch_name = ips.get(ip)
    #     if switch_name:
    #         print(host['hostid'], switch_name)
    #         update_hosts(zabbix_url, headers, auth_token, host['hostid'], ['位置', switch_name])

    logout(zabbix_url, headers, auth_token)

def test():
    host = get_hosts(zabbix_url, headers, auth_token, "Zabbix server")
    update_hosts(zabbix_url, headers, auth_token, host[0]['hostid'], ['aaa', 'bbb'])
    
if __name__ == "__main__":
    main()

class ZabbixHost:
    def __init__(self, zabbix_url, headers, auth_token):
        self.zabbix_url = zabbix_url
        self.headers = headers
        self.auth_token = auth_token

    def get_auth(self):
        pass

    def get_host(self, hostid):
        pass

    def update_tag(self, hostid, tags: list):
        pass
    
